/*
 * @lc app=leetcode.cn id=1262 lang=golang
 *
 * [1262] 可被三整除的最大和
 */
package Solutions

import "fmt"

// @lc code=start
func maxSumDivThree(nums []int) int {
	dp := [3]int{0, 0, 0}
	dp[nums[0]%3] = nums[0]
	for i := 1; i < len(nums); i++ {
		switch nums[i] % 3 {
		case 0:
			dp[0] += nums[i]
			dp[1] += nums[i]
			dp[2] += nums[i]
		case 1:
			if dp[2]%3 == 2 {
				dp[0] = max(dp[0], dp[2]+nums[i])
			}
			dp[1] = max(dp[1], dp[0]+nums[i])
			if dp[1]%3 == 1 {
				dp[2] = max(dp[2], dp[1]+nums[i])
			}
		case 2:
			if dp[1]%3 == 1 {
				dp[0] = max(dp[0], dp[1]+nums[i])
			}
			if dp[2]%3 == 2 {
				dp[1] = max(dp[1], dp[2]+nums[i])
			}
			dp[2] = max(dp[2], dp[0]+nums[i])
		}
		fmt.Println(dp)

	}
	return dp[0]
}

// @lc code=end
